package com.hnevc.jsonweatherdeam;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button mBtn1, mBtn2, mBtn3;
    TextView mTvCityName, mTvPm, mTvTemp, mTvWind, mTvWeather;
    ImageView mIvWeather;

    //定义保存weather链表
    List<Weather> weatherList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvCityName = (TextView) findViewById(R.id.id_tv_city_name);
        mTvPm = (TextView) findViewById(R.id.id_tv_pm);
        //……
        mIvWeather = (ImageView) findViewById(R.id.id_iv_weatherpng);

        mBtn1 = (Button) findViewById(R.id.id_btn_1);
        mBtn2 = (Button) findViewById(R.id.id_btn_2);
        mBtn3 = (Button) findViewById(R.id.id_btn_3);
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWeather(0);
            }
        });
        mBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWeather(1);
            }
        });
        mBtn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWeather(2);
            }
        });


        try {
            //解析json文件
            InputStream inputStream = getResources().openRawResource(R.raw.weather2);

            //构建一个字符串缓存
            StringBuffer sb = new StringBuffer();

            //读取字节信息过程中，需要的零时空间
            byte[] bytes = new byte[10];
            int len = 0;

            while ((len = inputStream.read(bytes)) != -1) {
                //bytes中的字节数据，转换为字符串，存放到sb中
                String s = new String(bytes, 0, len, "utf8");
                sb.append(s);
            }
            String data = sb.toString();
            //在调试区输出data
            Log.e("TAG", "onCreate: " + data);
            //Gson类解析
            //Gson gson =new Gson();
            //weatherList =gson.fromJson(data, new TypeToken<List<Weather>>(){}.getType());

            //解析
            try {
                //json字符串转换为json数组对象
                JSONArray jsonarray = new JSONArray(data);
                for (int i = 0; i < jsonarray.length(); i++) {
                    //获取第i个json对象
                    JSONObject object = jsonarray.getJSONObject(i);

                    String temp = object.getString("temp");
                    String name = object.optString("name");
                    String pm = object.getString("pm");
                    String wind = object.getString("wind");
                    String weather = object.getString("weather");

                    Weather w = new Weather();
                    w.setName(name);
                    w.setPm(pm);
                    w.setTemp(temp);
                    w.setWeather(weather);
                    w.setWind(wind);

                    //将w加入链表
                    weatherList.add(w);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void showWeather(int i) {
        Weather w = weatherList.get(i);
        mTvCityName.setText(w.getName());
        mTvPm.setText(w.getPm());
        mIvWeather.setImageResource(R.drawable.cloudy);


    }
}